
function TreeSelectRender(tree,displayDegree,padWidth,radioName){
	this.tree = tree ;
	this.id = TreeSelectRender.defaultInstanceName ;
	this.displayDegree = displayDegree==null?100:displayDegree ;
	this.displays = new Array(this.tree.nodes.size());
	this.opens = new Array(this.tree.nodes.size());
	for(var i=0;i<this.displays.length;i++){
		this.displays[i] = this.tree.nodes.get(i).rank <= this.displayDegree ;
		this.opens[i] = this.tree.nodes.get(i).rank < this.displayDegree ;
	}
	this.padWidth = padWidth==null?32:padWidth ;
	this.radioName = radioName==null?'radio':radioName ;
	return this ;
}

TreeSelectRender.defaultInstanceName = "TSR";

TreeSelectRender.prototype.bindById = function () {
    var key = "TreeSelectRender." + this.id;
    window[key] = this;
}

function findTreeSelectRenderInstance() {
    var key = "TreeSelectRender." + TreeSelectRender.defaultInstanceName;
    return window[key];
}


TreeSelectRender.prototype.refresh = function(){
//	this.tree.init();
	var html = this.getTreeHTML() ;
//	document.write(html);
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeSelectRender.prototype.render = function(){
	if(!this.tree.neatened){
		this.tree.init();
	}
	var html = this.getTreeHTML() ;
	document.write("<div id='DivTree' style='width:100%;height:100%;text-align:left;vertical-align:top;'></div>");
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeSelectRender.prototype.getTreeHTML = function(){
	var buffer = new StringBuffer();
	buffer.append("<table cellspacing=0 cellpadding=0>");
	for(var i=0;i<this.tree.nodes.size();i++){
		buffer.append(this.getTreeNodeHTML(this.tree.nodes.get(i)));
	}
	buffer.append("</table>");
	return buffer.toString();
}

TreeSelectRender.prototype.getTreeNodeHTML = function(node){
	var buffer = new StringBuffer();

	buffer.append("<tr id='").append(this.id).append("_ROW_").append(node.id).append("'");
	if(!this.displays[node.sequence]){
		buffer.append(" style=\"display:none;\"");
	}
	buffer.append(">");
	buffer.append("<td>");
	
	buffer.append("<table cellspacing=0 cellpadding=0");
	if(node.method != null && node.method != ""){
		buffer.append(" onclick=\"").append(node.method).append("\"");
	}
	buffer.append(">");
	buffer.append("<tr>");
	var lines = node.lines ;
	for(var i=0;i<lines.length;i++){
		if(lines[i] == "1"){
			buffer.append("<td><div class='YHMenu_nodeLine'></div></td>");
		}else{
			buffer.append("<td><div class='YHMenu_nodeEmpty'></div></td>");
		}
	}
	buffer.append("<td class='").append(this.getNodeCss(node)).append("' onclick=\"changeTreeView('").append(node.id).append("')\"><div style='width:16px;'></div></td>");
	buffer.append("<td><div class='YHMenu_nodeIcon'><img src='").append(node.icon).append("' class='menuIcon'></img></div></td>");
	buffer.append("<td class='YHMenu_nodeName' onclick=\"changeTreeView('").append(node.id).append("')\"><span class='spanNode1' onmouseover=\"this.className='spanNode2';\" onmouseout=\"this.className='spanNode1';\">");
	buffer.append(node.name);
	buffer.append("</span></td>");

	buffer.append("<td>");
	var len = node.name.length;
	if(node.sequence > 0){
		for(var i=1;i<(this.padWidth-len);i++){
			buffer.append('��');
		}
	}
	buffer.append("</td>");
	buffer.append("<td>");
	if(node.sequence > 0){
		buffer.append("<input type=radio class=radio name='").append(this.radioName).append("' style='height:12px;width:12px;'>");
	}
	buffer.append("</td>");

	buffer.append("</tr>");
	buffer.append("</table>");

	buffer.append("</td>");
	buffer.append("</tr>");
	
	return buffer.toString();
}

TreeSelectRender.prototype.getNodeCss = function(node){
	var open = this.opens[node.sequence]?'1':'0';
	var str = "YHMenu_node" + open + (node.hasYoungerBrother()?"1":"0") + (node.hasChild()?"1":"0");
	return str ;
}

function changeTreeView(id){
	var viewRender = findTreeSelectRenderInstance();
	var node = viewRender.tree.findNodeById(id);
	if(node.sequence >= node.shadowIndex){
		return ;
	}
	viewRender.opens[node.sequence] = !viewRender.opens[node.sequence] ;
	var status = viewRender.opens[node.sequence] ;
	var size = viewRender.tree.nodes.size();
	for(var i=(node.sequence+1);i<=node.shadowIndex;i++){
		if(i >= size){
			break ;
		}
		viewRender.displays[i] = status ;
		viewRender.opens[i] = status ;
	}
	viewRender.refresh();
}